
import React, { useState, useEffect } from 'react';
import Web3 from 'web3';
import { useParams, Link, useNavigate } from 'react-router-dom';
// import '../static/css/app.css'; // 导入 CSS 文件
// import '../static/assets/bootstrap/css/bootstrap.min.css';
// import '../static/assets/fonts/ionicons.min.css';
// import '../static/assets/css/pikaday.min.css';
import ActivityListABI from '../contract/ABIs/ActivityList.json'; // 活动列表合约 ABI
import UserListABI from '../contract/ABIs/UserList.json'; // 用户列表合约 ABI
import AdminABI from '../contract/ABIs/Admin.json'; // 管理员合约 ABI
import AdminNavBar from './nav/AdminNavBar.js';
import ActivityListAddress from '../contract/ADDRESSes/ActivityListAddress.js'; // 活动列表合约地址
import AdminListAddress from '../contract/ADDRESSes/AdminListAddress.js'; // 管理员列表合约地址
import UserListAddress from '../contract/ADDRESSes/UserListAddress.js'; // 用户列表合约地址
// import '../static/indexcss/aacsss.css';
import Eye from './Eye.js'
import Admin_integralNavBar from './nav/Admin_integralNavBar.js'
import gb from '../static/bootstrap/images/gb.svg';
import us from '../static/bootstrap/images/us.svg';
import user3 from '../static/bootstrap/images/user3.png';
import Head from './nav/Head.js';



const Admin_PublishActivity = () => {
    const [web3, setWeb3] = useState(null);
    // 使用 useParams 获取路由参数中的地址信息
    const { address } = useParams();
    const [contractActivityList, setcontractActivityList] = useState(null);
    const [contractUserList, setContractUserList] = useState(null);
    const [contractAdmin, setcontractAdmin] = useState(null);
    const [isAdmin, setIsAdmin] = useState(false);

    const [activityName, setactivityName] = useState('');
    const [activityCategory, setactivityCategory] = useState('竞赛类');
    const [activityContinue, setactivityContinue] = useState('');
    const [activityDescription, setactivityDescription] = useState('');
    const navigate = useNavigate();


    // 新增状态变量来存储用户积分 
    // const [userPoints, setUserPoints] = useState(0);

    useEffect(() => {
        async function init() {
            // 连接到以太坊网络
            if (window.ethereum) {
                const web3Instance = new Web3(window.ethereum);
                try {
                    // 请求用户授权
                    await window.ethereum.enable();
                    setWeb3(web3Instance);

                    // 创建活动列表合约实例
                    const activityListInstance = new web3Instance.eth.Contract(
                        ActivityListABI,
                        ActivityListAddress
                    );
                    setcontractActivityList(activityListInstance);

                    // 创建用户列表合约实例
                    const userListInstance = new web3Instance.eth.Contract(
                        UserListABI,
                        UserListAddress
                    );
                    setContractUserList(userListInstance);

                    // 创建管理员合约实例
                    const adminInstance = new web3Instance.eth.Contract(
                        AdminABI,
                        address
                    );
                    setcontractAdmin(adminInstance);

                    // 获取当前用户的以太坊账户地址
                    const accounts = await web3Instance.eth.getAccounts();
                    const currentAddress = accounts[0];
                    // 获取管理员合约的 owner 地址
                    const ownerAddress = await adminInstance.methods.getOwner().call();
                    // 检查当前账户是否是管理员本人
                    setIsAdmin(currentAddress === ownerAddress);
                } catch (error) {
                    console.error(error);
                }
            } else {
                console.error('请安装以太坊浏览器插件，如 MetaMask');
            }
        }
        init();
    }, [address]);


    // 发布活动函数
    const PublishActivity = async () => {
        if (contractActivityList) {
            try {
                // 获取当前用户的以太坊账户地址
                const accounts = await web3.eth.getAccounts();
                const fromAddress = accounts[0];
                console.log("fromAddress:", fromAddress, "Input:", activityName, activityCategory, activityContinue, activityDescription);

                // 调用智能合约的某个函数
                await contractActivityList.methods.publishActivity(activityName, AdminListAddress, address, activityCategory, activityContinue, activityDescription).send({ from: fromAddress });
                alert("发布成功")
                navigate(`/admin/${address}/Admin_integral/Admin_ActivityList`)
            } catch (error) {
                console.error(error);
                alert("发布失败")
            }
        }
    };



    return (
        <div>
            {/* 如果当前账户不是管理员，则显示提示信息 */}
            {!isAdmin && (<div>您没有权限访问此页面。</div>)}

            {/* 管理员界面 */}
            {isAdmin && (
                <div className="page-wrapper" style={{
                    backgroundColor: '#FFFFFF'
                }}>
                    <div className="main-container">
                        <AdminNavBar address={address}></AdminNavBar>

                        <div className="app-container">
                            <Head></Head>
                            <div className="app-body">
                                <div className="container-fluid">
                                    <div>
                                        <main className="page hire-me-page">
                                            <section className="portfolio-block hire-me">

                                                <div className="container">
                                                    <div className="border rounded border-0 shadow-lg p-3 p-md-5"
                                                        data-bs-theme="light"
                                                        style={{
                                                            backgroundColor: 'rgba(36, 36, 41, 0.6)'
                                                        }}
                                                    >

                                                        <h1>发布活动</h1>
                                                        <div class="mb-3">
                                                            <div className="mb-3">
                                                                <label className="form-label" htmlFor="name">名称:</label>
                                                                <input className="form-control item" id="name" type="text" placeholder="活动名" value={activityName} onChange={(e) => setactivityName(e.target.value)} />
                                                            </div>

                                                            <div className="mb-3">
                                                                <label className="form-label" htmlFor="subject">种类:</label>
                                                                <select className="form-select" id="subject" placeholder="活动种类" value={activityCategory} onChange={(e) => setactivityCategory(e.target.value)}
                                                                    style={{ background: '#89898C', border: '1px solid #ced4da', borderRadius: '0.25rem' }}>
                                                                    <option value="竞赛类">竞赛类</option>
                                                                    <option value="公益类">公益类</option>
                                                                    <option value="创新类">创新类</option>
                                                                    <option value="文化类">文化类</option>
                                                                </select>
                                                            </div>

                                                            <div className="mb-3">
                                                                <label className="form-label" htmlFor="name">持续时间：</label>
                                                                <input className="form-control item" id="name" type="text" placeholder="输入多少整数就是多少分钟" value={activityContinue} onChange={(e) => setactivityContinue(e.target.value)} />
                                                            </div>

                                                            <div className="mb-3">
                                                                <label className="form-label" htmlFor="name">活动描述:</label>
                                                                <input className="form-control item" id="name" type="text" placeholder="活动描述" value={activityDescription} onChange={(e) => setactivityDescription(e.target.value)} />
                                                            </div>

                                                            <div className="mb-3 mt-4">
                                                                <button className="btn btn-primary btn-lg d-block w-100" onClick={PublishActivity}>发布</button>
                                                            </div>



                                                        </div>
                                                    </div>
                                                </div>

                                            </section>
                                        </main>
                                    </div>
                                </div>
                            </div>
                            <div className="app-footer">
                                <span>
                                    © Bootstrap Gallery 2023
                                </span>
                            </div>
                        </div>
                    </div>

                </div>
            )}
        </div>
    );
}

export default Admin_PublishActivity;
